package com.zelin.cart.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.AddressEntity;
import com.zelin.entity.OrderEntity;
import com.zelin.entity.PayLogEntity;
import com.zelin.entity.group.Cart;
import com.zelin.order.service.AddressService;
import com.zelin.order.service.OrderService;
import com.zelin.pay.service.PayService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/14-15:23
 * ------------------------------
 */
@Controller
public class OrderController {

    @Reference(timeout = 5000)
    private OrderService orderService;
    @Reference(timeout = 5000)
    private AddressService addressService;
    @Reference(timeout = 5000)
    private PayService payService;
    //1. 得到订单地址列表及购物车列表
    @RequestMapping({"/","/cart.html"})
    public String getOrderList(Model model){
        //1.1 得到订单址列表
        //① 获取登录名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //② 根据登录名获取地址列表
        List<AddressEntity> addressEntities = addressService.getAddresList(name);

        //1.2 得到购物车列表
        List<Cart> carts = orderService.getCartList(name);

        //1.3 将上面的两组数据送到前端去
        model.addAttribute("addressList",addressEntities);
        model.addAttribute("cartList",carts);

        //1.4 返回视图谱
        return "getOrderInfo";
    }

    /**
     * 功能: 提交订单
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/8/14 16:44
     */

    @RequestMapping("/order/add")
    public String add(OrderEntity order,Model model){
        System.out.println("order = " + order);
        //1.1 得到订单址列表
        //① 获取登录名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //② 为订单对象设置属性
        order.setSourceType("2");
        order.setUserId(name);
        orderService.add(order);
        //1.2 根据用户的userid查询支付日志
        PayLogEntity paylog = payService.findPayLogByUserId(name);
        //1.3 将支付日志放到model中
        model.addAttribute("paylog",paylog);
        return "pay";
    }
}
